﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using ent;

public partial class controlpanel_files_crud : System.Web.UI.Page {
    public String URL = ApplicationCommon.getPageThis();

    public String UPLOAD_SESSION_ID = String.Empty;
    public String UPLOAD_USER_ID = "0";
    public String UPLOAD_MODE = String.Empty;
    public String UPLOAD_ID = "0";

    private String _qsMode = String.Empty;
    private String _qsReturn = String.Empty;
    private int _qsId = 0;

    private TableLists t3 = new TableLists();
    private TableFiles t4 = new TableFiles();
    private ServiceFiles s1 = new ServiceFiles();

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Page.Header.DataBind();
    }
    protected void Page_Load(Object sender, EventArgs e) {
        _qsMode = Request.QueryString["mode"];
        _qsReturn = Request.QueryString["return"];
        _qsId = System.Convert.ToInt32(Request.QueryString["id"]);
        getRequestJavaScript();

        // Mode.
        if(String.Equals(_qsMode, "add")) {
            viewAdd();
        } else if(String.Equals(_qsMode, "edit") && _qsId > 0) {
            viewEdit();
        } else if(String.Equals(_qsMode, "trash")) {
            viewTrash();
        } else {
            viewCrud();
        }
    }

#region Methods

    /// <summary>Get HTTP request from JavaScript postback.</summary>
    protected void getRequestJavaScript() {
        if(!Page.IsPostBack) {return;}

        String eventName = Request.Form["__EVENTTARGET"];
        String eventArgument = Request.Form["__EVENTARGUMENT"];

        if(String.Equals(eventName, "onMode")) {
            // Get multiple values from serialized string.
            System.Collections.Specialized.NameValueCollection aryEventArguments = System.Web.HttpUtility.ParseQueryString(eventArgument);
            // Set mode by query string.
            if(String.Equals(aryEventArguments["action"], "add")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "add");
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else if(String.Equals(aryEventArguments["action"], "edit")) {
                if(!ApplicationCommon.isEmpty(aryEventArguments["id"])) {
                    Hashtable qs1 = new Hashtable();
                    qs1.Add("mode", "edit");
                    qs1.Add("id", System.Convert.ToInt32(aryEventArguments["id"]));
                    Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
                }
            } else if(String.Equals(aryEventArguments["action"], "trash")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "trash");
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else {
                Response.Redirect(URL);
            }
        }
    }
    /// <summary>Init form input values.</summary>
    protected void initFormInputValues() {
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            ucFile.prop_hidFileId.Value = "0";
            // Get list: file type.
            ucFile.prop_ddlFileType.DataSource = t3.getList((int)TableLists.enumList.FileTypes, null);
            ucFile.prop_ddlFileType.DataTextField = TableLists.TBL__lists__list_name;
            ucFile.prop_ddlFileType.DataValueField = TableLists.TBL__lists__list_value;
            ucFile.prop_ddlFileType.DataBind();
            ucFile.prop_ddlFileType.Items.Insert(0, new ListItem("", "0"));
        }
        ucFile.prop_txtFileExtension.Enabled = false;
        ucFile.prop_ddlFileType.Enabled = false;
    }
    /// <summary>View in CRUD mode.</summary>
    protected void viewCrud() {
        Master.prop_plhHeadYui.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableCrud.Visible = true;
        plhCrud.Visible = true;
    }
    /// <summary>View in add mode.</summary>
    protected void viewAdd() {
        Master.prop_plhHeadYui.Visible = true;
        Master.prop_plhHeadUpload.Visible = true;
        plhJavaScriptFormSpecial.Visible = true;
        plhAdd.Visible = true;
        plhForm.Visible = true;
        // Init form input values.
        initFormInputValues();
        // Set upload paramenters.
        UPLOAD_SESSION_ID = Session.SessionID;
        UPLOAD_USER_ID = Session[ApplicationCommon.SESSION__user_id].ToString();
        UPLOAD_MODE = _qsMode;
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            // List type controls goes here.
        }
    }
    /// <summary>View in edit mode.</summary>
    protected void viewEdit() {
        Master.prop_plhHeadYui.Visible = true;
        Master.prop_plhHeadUpload.Visible = true;
        plhJavaScriptFormSpecial.Visible = true;
        plhEdit.Visible = true;
        plhForm.Visible = true;
        DataRow dr1 = t4.getFile(_qsId);
        // Set upload paramenters.
        UPLOAD_SESSION_ID = Session.SessionID;
        UPLOAD_USER_ID = Session[ApplicationCommon.SESSION__user_id].ToString();
        UPLOAD_MODE = _qsMode;
        UPLOAD_ID = _qsId.ToString();
        // Init form input values.
        initFormInputValues();
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            ucFile.prop_txtFileName.Text = (String)dr1[TableFiles.TBL__files__file_name];
            ucFile.prop_txtFileDescription.Text = (String)dr1[TableFiles.TBL__files__file_description];
            ucFile.prop_txtFileExtension.Text = (String)dr1[TableFiles.TBL__files__file_extension];
            ucFile.prop_ddlFileType.Items.FindByValue(dr1[TableFiles.TBL__files__file_type].ToString()).Selected = true;
        }
    }
    /// <summary>View in trash mode.</summary>
    protected void viewTrash() {
        Master.prop_plhHeadYui.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableEdit.Visible = true;
        plhTrash.Visible = true;
    }
    /// <summary>Return user from (previous page).</summary>
    protected bool returnUserFrom() {
        String _qsReturn = Request.QueryString["return"];
        if(!ApplicationCommon.isEmpty(_qsReturn)) {
            Response.Redirect(HttpUtility.UrlDecode(_qsReturn));
        }
        return false;
    }

#endregion

#region Events

    protected void btnFormOkay_Click(Object sender, EventArgs e) {
        if(plhAdd.Visible && ucFile.prop_fuFile.HasFile) {
            Hashtable p1 = new Hashtable();
            p1.Add(TableFiles.TBL__files__file_uploader_user_id, (int)Session[ApplicationCommon.SESSION__user_id]);
            p1.Add(TableFiles.TBL__files__file_name, ucFile.prop_txtFileName.Text);
            p1.Add(TableFiles.TBL__files__file_name_slug, TableFiles.toSlug(ucFile.prop_txtFileName.Text));
            p1.Add(TableFiles.TBL__files__file_description, ucFile.prop_txtFileDescription.Text);
            p1.Add(TableFiles.TBL__files__file_binary, ucFile.prop_fuFile.PostedFile);
            p1.Add(TableFiles.TBL__files__file_extension, System.IO.Path.GetExtension(ucFile.prop_fuFile.PostedFile.FileName).Substring(1).ToLower());
            p1.Add(TableFiles.TBL__files__file_size_bytes, ucFile.prop_fuFile.PostedFile.ContentLength);
            p1.Add(TableFiles.TBL__files__file_date_created, DateTime.Now);
            t4.createFile(p1);
        } else if(plhEdit.Visible || !String.Equals(ucFile.prop_hidFileId.Value, "0")) {
            Hashtable p2 = new Hashtable();
            p2.Add(TableFiles.TBL__files__file_name, ucFile.prop_txtFileName.Text);
            p2.Add(TableFiles.TBL__files__file_name_slug, TableFiles.toSlug(ucFile.prop_txtFileName.Text));
            p2.Add(TableFiles.TBL__files__file_description, ucFile.prop_txtFileDescription.Text);
            
            if(String.Equals(ucFile.prop_hidFileId.Value, "0")) {
                t4.dynamicSqlUpdate(p2, TableFiles.TBL__files, TableFiles.TBL__files___PK__file_id + " = " + _qsId);
            } else {
                t4.dynamicSqlUpdate(p2, TableFiles.TBL__files, TableFiles.TBL__files___PK__file_id + " = " + Convert.ToInt32(ucFile.prop_hidFileId.Value));
            }
        }
        if(!returnUserFrom()) {
            Response.Redirect(URL);
        }
    }
    protected void btnFormCancel_Click(Object sender, EventArgs e) {
        if(!returnUserFrom()) {
            Response.Redirect(URL);
        }
    }

#endregion

}